﻿//using UnityEngine;
//using System.Collections;

//public class EnemyAIController2D : RaycastCharacterInput,IEnemy
//{
//    public BoxCollider2D[] geometry;
//    public RaycastCharacterController2D targetController;
//    public RaycastCharacterController2D meController;
//    public float xDelta;
//    public float yDelta;
//    public float jumpHeight;
//    public float jumpDistance;
//    public float decideTime;

//    private float moveDirection;
//    private Transform target;
//    private Transform me;
//    private float climbTargetPosition;
//    private float goalPosition;
//    private float minX, minY, maxY, maxX;
//    void Start()
//    {
//        target = targetController.transform;
//        me = transform;
//        InvokeRepeating("Decide", decideTime, decideTime);
       
//    }

//    void Decide()
//    {
       
//       // Debug.Log("Method Decide");
//        x = 0.0f;
//        y = 0.0f;
//        jumpButtonDown = false;
//        jumpButtonHeld = false;
//        if (meController.State == CharacterState.JUMPING || meController.State == CharacterState.FALLING || meController.State == CharacterState.AIRBORNE)
//        {
//            x = moveDirection / 1.1f;
//        }
//        else
//        {
//            AIAction currentAction = DecideOnAction();
          
//            switch (currentAction)
//            {
//                case (AIAction.JUMP_TO_PLATFORM):
//                    if (Mathf.Abs(goalPosition - me.position.x) < xDelta)
//                    {
//                        x = moveDirection / 1.1f;
//                        jumpButtonDown = true;
//                    }
//                    else {
//                        if (goalPosition > me.position.x)
//                        {
//                            x = 1f / 1.1f;
//                        }
//                        else
//                        {
//                        x = -1f / 1.1f;
//                        }
//                    }
//                    break;
//                case(AIAction.JUMP_TO_CHARACTER):
//                    x = moveDirection;
//                    jumpButtonDown = true;
//                    break;
        
//                case(AIAction.WALK_TOWARDS_TARGET):
//                    x = moveDirection;
//                    break;

//            }
//        }
//    }

//    private enum AIAction
//    {
//        NONE,
//        WALK_TOWARDS_TARGET,
//        FALL_OFF_LADGE,
//        JUMP_TO_PLATFORM,
//        JUMP_TO_CHARACTER
//    }

//    private AIAction DecideOnAction()
//    {
//        BoxCollider2D ClimbTarget = null;
//        climbTargetPosition = me.transform.position.x;
//        BoxCollider2D current = null;
//        foreach (RaycastCollider2D feetCollider in meController.feetColliders)
//        {
//            RaycastHit2D collision = feetCollider.GetCollision(1 << meController.backgroundLayer | 1 << meController.passThroughLayer, 0.01f);

//            if (collision.collider != null && collision.collider is BoxCollider2D)
//            {
//                current = (BoxCollider2D)collision.collider;
//                break;
//            }
       
//        }
//        if ((target.position.y > transform.position.y) && (targetController.GroundedFeetCount > 0 && target.position.y > transform.position.y + jumpHeight + yDelta && (meController.State != CharacterState.JUMPING && meController.State != CharacterState.FALLING && meController.State != CharacterState.AIRBORNE)))
//        {
//            if (current != null)
//            {
              
//                float hightest = -9999;
//                float closest = 9999;
                
//                foreach (BoxCollider2D c in geometry)
//                {
                   
//                    if (c != current && Max(c).y < Max(current).y + jumpHeight)
//                    {
//                        if (Min(c).x > Min(current).x && Min(c).x < Min(current).x + jumpDistance)
//                        {
//                            if (Max(c).y > hightest)
//                            {
//                                climbTargetPosition = Mathf.Min(Max(current).x, (Min(c).x- jumpDistance /2f));
//                                ClimbTarget = c;
//                                hightest = Max(c).y;
//                                closest = Mathf.Abs(climbTargetPosition - me.position.x);
//                                moveDirection= 1f;

//                            }
//                        }
//                        if (Max(c).x < Max(current).x && Max(c).x > Min(current).x - jumpDistance)
//                        {
//                            if (hightest <Max(c).y)
//                            {
//                                climbTargetPosition = Mathf.Max(Min(current).x,Max(c).x + jumpDistance/2f);
//                                ClimbTarget = c;
//                                hightest = Max(c).y;
//                                moveDirection = -1;
//                            }
//                            else if (ClimbTarget == c && closest > Mathf.Abs(Mathf.Max(Min(current).x, Max(c).x + (jumpDistance/2f)- me.position.x)))
//                            {
//                                climbTargetPosition =Mathf.Max(Min(current).x, Max(c).x + (jumpDistance/2f));
//                                moveDirection = -1;


//                            }
//                        }
//                    }
//                }
//            }
//        }
//        if (ClimbTarget != null)
//        {
//            goalPosition = climbTargetPosition;
//            return AIAction.JUMP_TO_PLATFORM;
//        }
//        float enemyDirection;

//        if (target.position.x > me.position.x)
//        {
//            enemyDirection = 1.0f;
//        }
//        else
//        {
//            enemyDirection = -1.0f;
//        }
//        if (targetController.GroundedFeetCount == 0 && target.position.y > me.position.y && Mathf.Abs(target.position.x - me.position.x) > xDelta * 3.0f)
//        {
//            moveDirection = 0;
//            return AIAction.JUMP_TO_CHARACTER;
//        }

//        if (current != null && target.position.y < me.position.y + yDelta)
//        {
//            foreach (BoxCollider2D c in geometry)
//            {
//            //    Debug.Log("bottom");
//                Debug.Log(Min(c).x + "||" + Max(c).x);
//                if (Max(c).x < Max(current).x && Max(c).x >Min(current).x && Max(c).y < Max(current).y)
//                {
//                    moveDirection = -1.0f;
//                    return AIAction.FALL_OFF_LADGE;
//                }
//                else if (Min(c).x < Max(current).x && Min(c).x > Min(current).x && Max(c).y < Max(current).y)
//                {
//                    moveDirection = 1f;
//                    return AIAction.FALL_OFF_LADGE;
//                }
//            }
//        }
//        if ((Mathf.Abs(target.position.x - me.position.x) > xDelta))
//        {
//            moveDirection = enemyDirection;
//            return AIAction.WALK_TOWARDS_TARGET;
//        }

//        return AIAction.NONE;
//    }
//    public  Vector2 Max(this Collider2D collider)
//    {
//        if (collider is BoxCollider2D)
//            return (Vector2)collider.gameObject.transform.position +
//            new Vector2(collider.gameObject.transform.localScale.x * (((BoxCollider2D)collider).center.x + (((BoxCollider2D)collider).size.x / 2)),
//                        collider.gameObject.transform.localScale.y * (((BoxCollider2D)collider).center.y + (((BoxCollider2D)collider).size.y / 2)));
//        Debug.LogError("Unable to determine max for this collider");
//        return Vector2.zero;
//    }

//    public  Vector2 Min(this Collider2D collider)
//    {
//        if (collider is BoxCollider2D) return (Vector2)collider.gameObject.transform.position +
//            new Vector2(collider.gameObject.transform.localScale.x * (((BoxCollider2D)collider).center.x - (((BoxCollider2D)collider).size.x / 2)),
//                        collider.gameObject.transform.localScale.y * (((BoxCollider2D)collider).center.y - (((BoxCollider2D)collider).size.y / 2)));

//        Debug.LogError("Unable to determine min for this collider");
//        return Vector2.zero;
//    }
//    //protected Rect bounds(BoxCollider2D collider)
//    //{
      
//    //    minX = collider.transform.TransformPoint(collider.center -collider.size*0.5f).x;
//    //    maxX = collider.transform.TransformPoint(collider.center + collider.size*0.5f).x;

//    //     minY = collider.transform.TransformPoint(collider.center - collider.size * 0.5f).y;
//    //    maxY = collider.transform.TransformPoint(collider.center + collider.size * 0.5f).y;

       
//    //    return new Rect(minX,maxY,maxX,minY);
       
//    //}
//    public void Kill()
//    {
//        throw new System.NotImplementedException();
//    }

//    public void KillFromBelow(float force)
//    {
//        throw new System.NotImplementedException();
//    }

//    public void KillFromAbove(HitBox other, Collider me)
//    {
//        throw new System.NotImplementedException();
//    }
//}